package com.springmvc.netty.action;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.springmvc.cache.CacheKey;
import com.springmvc.cache.MapCacheManager;
import com.springmvc.netty.command.Command;
import com.springmvc.netty.core.ActionMap;
import com.springmvc.netty.core.NettyController;
import com.springmvc.utils.mj.MJRoom;
import com.springmvc.utils.mj.RoomUtil;
import com.springmvc.web.majiang.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@NettyController()
public class RoomAction extends BaseAction {

	@Autowired
	private IUserService userService;

	private static MapCacheManager cache = MapCacheManager.getInstance();

	@ActionMap(Command.MJ1001)
	public void MJ1001(SocketIOServer server, SocketIOClient client, HashMap<String, String> data,
			AckRequest ackRequest) {

		String roomid = data.get("roomid");

		RoomUtil.initRoom(roomid);

		sendRoomEvent(Command.MJ1001, server, roomid, cache.get(CacheKey.MJRoom(roomid)));
		// sendEvent(Command.MJ1001, client, cache.get(CacheKey.MJRoom(roomid)));
	}

	@ActionMap(Command.MJ1002)
	public void MJ1002(SocketIOServer server, SocketIOClient client, HashMap<String, String> data,
			AckRequest ackRequest) {
		String roomid = data.get("roomid");
		sendEvent(Command.MJ1002, client, cache.get(CacheKey.MJRoom(roomid)));
	}

	@ActionMap(Command.MJ1003)
	public void MJ1003(SocketIOServer server, SocketIOClient client, HashMap<String, String> data,
			AckRequest ackRequest) {
		String roomid = data.get("roomid");
		String userno = data.get("userno");
		String mj = data.get("mj");
		MJRoom mjRoom = (MJRoom) cache.get(CacheKey.MJRoom(roomid));

		if (!mjRoom.getUsermj().get(userno).contains(mj)) {
			return;
		}

		mjRoom.setPlaymj(mj);
		mjRoom.setPlaytype("4");
		mjRoom.getCentermj().add(mj);
		mjRoom.getUsermj().get(userno).remove(mj);

		sendRoomEvent(Command.MJ1003, server, roomid, cache.get(CacheKey.MJRoom(roomid)));
	}

	@ActionMap(Command.MJ1004)
	public void MJ1004(SocketIOServer server, SocketIOClient client, HashMap<String, String> data,
			AckRequest ackRequest) {
		String roomid = data.get("roomid");
		String userno = data.get("userno");
		String mj = data.get("mj");
		// String mmj = data.get("mmj");
		MJRoom mjRoom = (MJRoom) cache.get(CacheKey.MJRoom(roomid));
		// mjRoom.setPlaymj(mj);
		// mjRoom.setPalytype("4");
		mjRoom.getCentermj().remove(mjRoom.getCentermj().size() - 1);
		String[] mjArray = mj.split("\\|");
		for (int i = 0; i < mjArray.length - 1; i++) {
			mjRoom.getUsermj().get(userno).remove(mjArray[i]);
		}
		if (mjArray.length == 4) {
			mjRoom.getUsermj().get(userno).add(mjRoom.lastMj());
		}
		mjRoom.setPlaytype("2");
		mjRoom.getUsermmj().get(userno).add(mj);
		sendRoomEvent(Command.MJ1004, server, roomid, cache.get(CacheKey.MJRoom(roomid)));
	}

	@ActionMap(Command.MJ1005)
	public void MJ1005(SocketIOServer server, SocketIOClient client, HashMap<String, String> data,
			AckRequest ackRequest) {
		String roomid = data.get("roomid");
		String userno = data.get("userno");
		String mj = data.get("mj");
		MJRoom mjRoom = (MJRoom) cache.get(CacheKey.MJRoom(roomid));
		mjRoom.getCentermj().remove(mjRoom.getCentermj().size() - 1);
		String[] mjArray = mj.split("\\|");
		for (int i = 0; i < mjArray.length; i++) {
			if (!mjRoom.getPlaymj().equals(mjArray[i])) {
				mjRoom.getUsermj().get(userno).remove(mjArray[i]);
			}
		}
		mjRoom.setPlaytype("2");
		mjRoom.getUsermmj().get(userno).add(mj);
		sendRoomEvent(Command.MJ1005, server, roomid, cache.get(CacheKey.MJRoom(roomid)));
	}

	@ActionMap(Command.MJ1006)
	public void MJ1006(SocketIOServer server, SocketIOClient client, HashMap<String, String> data,
			AckRequest ackRequest) {
		String roomid = data.get("roomid");
		String userno = data.get("userno");

		String mj = data.get("mj");

		MJRoom mjRoom = (MJRoom) cache.get(CacheKey.MJRoom(roomid));
		mjRoom.getCentermj().remove(mjRoom.getCentermj().size() - 1);

		List<String> list1 = new ArrayList<String>();
		String[] mjArray = mj.split("\\|");
		for (int i = 0; i < mjArray.length; i++) {
			System.out.println("明牌：" + mjArray[i]);
			list1.add(mjArray[i]);
		}
		List<String> list2 = mjRoom.getUsermj().get(userno);
		for (String string : list2) {
			list1.add(string);
		}

		if (list1.toString().contains("条") && list1.toString().contains("饼") && list1.toString().contains("万")) {
			System.err.println("成功");
		}

		for (int i = 0; i < list1.size(); i++) {
			int n = 0;
			String mj1 = list1.get(i);
			for (int j = 0; j < list1.size(); j++) {
				String mj2 = list1.get(j);
				if (mj1.equals(mj2)) {
					n++;
				}
			}
			if (n >= 3) {
				System.err.println("成功");
			}
			// "1中", "2發", "3口"
			if (n == 2 && mj1.contains("0") || "1中".equals(mj1) || "2發".equals(mj1) || "3口".equals(mj1)) {
				System.err.println("成功");
			}
			n = 0;
		}
		
		
		
		for (int i = 0; i < list2.size(); i++) {
			String mj1 = list1.get(i);
			if(mj1.contains("条")) {
				
			}
		}

		mjRoom.setPlaytype("2");
		mjRoom.getUsermmj().get(userno).add(mj);
		sendRoomEvent(Command.MJ1006, server, roomid, cache.get(CacheKey.MJRoom(roomid)));
	}

}